<?php

abstract class Adept_Db_TableInfo
{

    protected $tableName;

    /**
     * @var Adept_Db_IConnection 
     */
    protected $connection;

    protected $columns;
    protected $columnsCount;

    protected $primaryKey;

    public function __construct($tableName, $connection = null)
    {
        $this->tableName = $tableName;
        if (is_null($connection) || is_string($connection)) {
            $this->connection = Adept_Db_Factory::getConnection($connection);
        } elseif ($connection instanceof Adept_Db_IConnection) {
            $this->connection = $connection;
        } else {
            throw new Adept_Exception('Invalid type of connection parameter ');
        }
    }

    abstract protected function retriveMetadata();

    /**
     * Returns column information object
     *
     * @param string $columnName
     * @return Adept_Db_ColumnInfo
     */
    public function getColumnInfo($columnName)
    {
        if (!isset($this->columns)) {
            $this->retriveMetadata();
        }
        return $this->columns[$columnName];
    }

    public function getColumnsInfo()
    {
        if (!isset($this->columns)) {
            $this->retriveMetadata();
        }
        return $this->columns;
    }

    public function hasColumn($column)
    {
        if (!isset($this->columns)) {
            $this->retriveMetadata();
        }
        return isset($this->columns[$column]);
    }

    public function getPrimaryKey()
    {
        if (!isset($this->columns)) {
            $this->retriveMetadata();
        }
        return $this->primaryKey;
    }

}
